import {
    refundDetail,
    actionRefund,
    orderSendGoods,
    getAddressList,
    actionRefused,
    actionRefundSuccess
} from '../services/api';
import {message} from 'antd';

export default {
    namespace: 'refundDetail',

    state: {
        status_records: [],
        order_goods: {},
        order: {},
        addressList: []
    },

    effects: {
        *fetch({payload}, {call, put}) {
            const response = yield call(refundDetail, payload);
            yield put({
                type: 'save',
                payload: response,
            });
        },
        *fetchAddress({payload}, {call, put}) {
            const response = yield call(getAddressList, payload);
            yield put({
                type: 'save',
                payload: {
                    addressList: response
                },
            });
        },
        *sureRefund({payload}, {call, put}) {
            const response = yield call(actionRefund, payload);
            if (response) {
                message.success('成功');
                let status = payload.address_info ? 202 : 203; //修改成功后的状态,用于更新视图
                yield put({
                    type: 'saveChangeInfo',
                    payload: {
                        status: status,
                        response,
                    },
                });
            } else {
                message.error('失败');
            }
        },
        *refused({payload}, {call, put}) { //拒绝申请
            const response = yield call(actionRefused, payload);
            if (response) {
                message.success('成功');
                yield put({
                    type: 'saveChangeInfo',
                    payload: {
                        status: 206,
                        response,
                    },
                });
            } else {
                message.error('失败');
            }
        },
        *refundSuccess({payload}, {call, put}) { //财务打款
            const response = yield call(actionRefundSuccess, payload);
            if (response) {
                if(response.status === 205){
                    message.success('成功');
                }else{ //207
                    message.error('失败:' + response.remark);
                }

                yield put({
                    type: 'saveChangeInfo',
                    payload: {
                        status: response.status,
                        response,
                    },
                });
            } else {
                message.error('失败');
            }
        },
        *sendGoods({payload}, {call, put}) {
            const response = yield call(orderSendGoods, payload);
            if (response) {
                message.success('成功');
                yield put({
                    type: 'saveSendGoods',
                    payload: payload,
                });
            } else {
                message.error('失败');
            }
        },
    },

    reducers: {
        save(state, {payload}){
            return {
                ...state,
                ...payload
            }
        },
        saveChangeInfo(state, {payload}){
            let status_records = [...state.status_records];
            status_records.push(payload.response);
            return {
                ...state,
                status_records,  //更新记录
                status: payload.status, //更新状态
            }
        },
        saveSendGoods(state, {payload}){
            let goods_list = [...state.goods_list];
            const {ids, express, express_no} = payload;
            const idsArr = ids.split(',');
            goods_list.forEach(v=> {
                idsArr.forEach(id=> {
                    if (id == v.id) {
                        v.status = 103;
                        v.express = express;
                        v.express_order = express_no;
                    }
                })
            });
            return {
                ...state,
                goods_list,
            }
        }
    },
};
